package name.marz.to.go.ahg.mhbk.executer;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import name.marz.to.go.ahg.common.util.HttpUtil;
import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.Parameters;
import name.marz.to.go.ahg.core.checker.DocumentChecker;
import name.marz.to.go.ahg.core.executer.AbstractLoginExecuter;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.nodes.Document;

public class MHBKLoginExecuter extends AbstractLoginExecuter {
	private final Document loginSrcDoc;
	private DocumentChecker checker = new MHBKLoginChecker();

	public MHBKLoginExecuter(URI uri,AutoGetterHttpClient client, Document doc ) {
		super(uri, client);
		this.loginSrcDoc = doc;
	}
	@Deprecated
	public MHBKLoginExecuter(Document srcDoc, AutoGetterHttpClient client) {
		super(null, client);
		this.loginSrcDoc = srcDoc;
		super.uri = HttpUtil.createURISafety(loginSrcDoc.getElementsByTag("form").select("[name=FORM1]").first().attr("action"));
	}
	
	
	@Override
	protected List<NameValuePair> getRequestParameters() {
		Parameters p = HttpUtil.getParametersFromInputTags(loginSrcDoc);
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("PASSWD_LoginPwdInput", super.userPass));
		params.add(p.getNameValuePair("_FRAMEID"));
		params.add(p.getNameValuePair("_TARGETID"));
		params.add(p.getNameValuePair("_LUID"));
		params.add(p.getNameValuePair("_TOKEN"));
		params.add(p.getNameValuePair("_FORMID"));
		params.add(p.getNameValuePair("_SUBINDEX"));
		params.add(p.getNameValuePair("POSTKEY"));
		return params;
	}

	@Override
	protected DocumentChecker getChecker() {
		return checker;
	}
}